/** 
 * 依赖于smartpaginator
 * 分页
*/  
(function($) {     // 创建一个闭包  
	 // 插件的定义    
   $.fn.AjaxPager = function(options,datatable) { // 插件的定义 
	    var opts; var $result;
	    if(datatable)
		{
	    	opts = $.extend({}, $.fn.AjaxPager.defaults, datatable.options,options);
	    	$result = datatable.table;
		}else
		{
			opts = $.extend({}, $.fn.AjaxPager.defaults, options);
			$result = $(options.result);
		}
 
		return this.each(function() {
			$this = $(this); 
	 
			if(opts.total && opts.total>0)
			{
				$this.smartpaginator({ 
					totalrecords: opts.total, 
					recordsperpage: opts.pagesize,
					initval:opts.page,
					theme:"rvblue",
					controlsalways:true,
					onchange: function(newPage, recordStartIndex, recordsEndIndex) {
						ajaxQuery(opts,$result);
					}
				});// end smartpaginator
			}else{
				$this.html(opts.norecords);
			}
			 
	    }); // end this.each
   };
   
   // 查询结果
   $.fn.AjaxPage = function(opts) {
	   var options = $.extend({}, $.fn.AjaxPager.defaults, opts);
	   var $this = $(this);
	   this.each(function() {
		   $.ajax({
		 		type: "POST",
		 		url:  options.url,//提交地址 
		 		data: $(options.form).serializeArray(),//参数 
		 		//contentType: "application/json; charset=utf-8", // 以普通方式提交
		 		success: function(responseText){
		 			$this.html(responseText); // 显示data
		 		},
		 		error:function (XMLHttpRequest, textStatus, errorThrown){
		 			$this.html(options.error);
		 		}
	      });
	   });
	   var ret = {"options":options,"table":this};
	   return ret;
   };

  // 查询结果
  function ajaxQuery(options,$result) {
	$.ajax({
		type: "POST",
		url:  options.url,//提交地址 
		data: $(options.form).serializeArray(),//参数 
		//contentType: "application/json; charset=utf-8", // 以普通方式提交
		success: function(responseText){
			$(this).show();
			$result.html(responseText); // 显示data
		},
		error:function (XMLHttpRequest, textStatus, errorThrown){
			$result.html(options.error);
 
		}
    });
  };
   
   // 插件的defaults    
  $.fn.AjaxPager.defaults = {
    process: '查询中...',    
	norecords:"没有查询到任何记录。",
	error:"出现错误",
    total: 0,
    pagesize:10,
    page:1,
	form:"",
	url:"",
	result:""
  };
// 闭包结束    
})(jQuery); 